#include "PlayState.h"


PlayState::PlayState()
{
	mMouseUp=false;
	mMouseDown=false;
	int choosedColor=Hexa::BLUE;
	mThisGameTurn=choosedColor;
}

PlayState::~PlayState()
{

}

void PlayState::OnEvent(SDL_Event *Event)
{
	GameState::OnEvent(Event);
	switch (Event->type)
    {
      case SDL_MOUSEBUTTONDOWN:
		  {
			mMouseDown=true;
			if(mIsGameOver)
			{
				CleanGamePlay();
				InitGamePlay();
			}
		  }
		  break;
      case SDL_MOUSEBUTTONUP:
		  {
			 if(mMouseDown)
			 {
				mMouseUp=true;
			 }
		  }
		  break;
	}
}

bool PlayState::OnInit(SDL_Surface* p_SurfDisplay)
{
	std::cout<<" PlayState::OnInit() PlayState::OnInit() "<<std::endl;

	SDL_ShowCursor(0);

	mImage1=GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\hexa1.png",1,1,1);
	mImage2=GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\test7.png",1,1,1);
	mImage3=GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\back.png",1,1,1);
	mImage4=GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\hexa2.png",1,1,1); //shiny

	mImgBlue=GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\hexaBlue.png",1,1,1);
	mImgGreen=GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\hexaGreen.png",1,1,1);

	// Score
	mImgGearBlue  = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearBlue.png",1,1,1);
	mImgGearGreen = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearGreen.png",1,1,1);

	// next number
	//mImgNextGreen	 = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\hexaGreenBig.png",1,1,1);
	//mImgNextBlue	 = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\hexaBlueBig.png",1,1,1);

	InitAnim(p_SurfDisplay);
	mDisplayPtr=p_SurfDisplay;
	InitGamePlay();

	///test anim
	animObj= new AnimatedObject();
	animObj->setScreen(*p_SurfDisplay);
	animObj->AddFrame(mImage1);
	animObj->AddFrame(mImgBlue);
	animObj->AddFrame(mImgGreen);

	

	return true;
}

void PlayState::OnLoop()
{
	CheckCollision(mCursorXValue,mCursorYValue);

	if((mNClosedTile>=120) && (!mIsGameOver))
	{
		mIsGameOver=true;
		mNClosedTile=0;
		if(HexaTile::ColoredBlue > HexaTile::ColoredGreen)
		{
				mYouWin->setVisibleWhenNotPlaying(true);
				mYouWin->setPlaying();
		}
		else if(HexaTile::ColoredBlue < HexaTile::ColoredGreen)
		{
				mYouLose->setVisibleWhenNotPlaying(true);
				mYouLose->setPlaying();
		}
		else if(HexaTile::ColoredBlue == HexaTile::ColoredGreen)
		{
				mGameDraw->setVisibleWhenNotPlaying(true);
				mGameDraw->setPlaying();
		}

		return;
	}
	if(mIsGameOver)
		return;
	//CheckNeighBorTiles(37);
	if(mPointedTile<0)
	{
			return;
	}
	if(mThisGameTurn==mGameTurn)  // MY TURN
	{
		//srand(time(NULL)*rand());
	    //randvalue=(rand()%19)+1;
		if(mMouseDown && mMouseUp)
		{
			mMouseDown=mMouseUp=false;
			if(HexaTiles.at(mPointedTile)->getColor()==Hexa::SHINY)
			{
				if(mPointedTile>-1)
				{
					//mThisGameTurn=Hexa::GREEN;
					cout<<"[ Human  ] Number Now : "<<randValue_A[Index_RandValue_A]<<endl;
					if(mGameTurn==Hexa::BLUE)
					{
						HexaTiles.at(mPointedTile)->setNumber(randValue_A[Index_RandValue_A++]);//randValues[nextRandValuesIndex++]);
						HexaTiles.at(mPointedTile)->setColor(Hexa::BLUE);
						CheckNeighBorTiles(mPointedTile);

						mGameTurn=Hexa::GREEN;
						mNextGreenAnim->setPlaying();
						mNClosedTile++;
					}
					else if(mGameTurn==Hexa::GREEN)
					{
						HexaTiles.at(mPointedTile)->setNumber(randValue_A[Index_RandValue_A++]);//(randValues[nextRandValuesIndex++]);
						HexaTiles.at(mPointedTile)->setColor(Hexa::GREEN);
						CheckNeighBorTiles(mPointedTile);

						mGameTurn=Hexa::BLUE;
						mNextBlueAnim->setPlaying();
						mNClosedTile++;
					}
				}
			}
		} // if(mMouseDown && mMouseUp)
	}//
	else  // ENEMY TURN
	{
		//cout<<"Next Number: "<<randvalue<<endl;
		if(mIntroStop==0)
		{
			mIntroStop=SDL_GetTicks();
			//srand(time(NULL)*rand());
			randvalue=(randValue_B[Index_RandValue_B++]);//randValues[nextRandValuesIndex++];//(rand()%19)+1;
		}
		if(SDL_GetTicks()-mIntroStop<800)
			return;

		mIntroStop=0;
		//srand(time(NULL)*rand());
		
		int TileChoosed=SolveTargetTile(randvalue);//(rand()%119);
		//std::cout<<" Thinking...: "<<TileChoosed;
		/* int v=HexaTiles.at(TileChoosed)->getColor();
		switch(v)
		{
		case Hexa::NORMAL:
			std::cout<<" NORMAL...: ";
			break;
		case Hexa::BLUE:
			std::cout<<" BLUE...: ";
			break;
		case Hexa::GREEN:
			std::cout<<" GREEN...: ";
			break;
		case Hexa::SHINY:
			std::cout<<" SHINY...: ";
			break;
		default:
			std::cout<<" UNKNOW... "<<v<<std::endl;
		}
		*/
		if(HexaTiles.at(TileChoosed)->getColor()==Hexa::NORMAL || HexaTiles.at(TileChoosed)->getColor()==Hexa::SHINY)
			{
				if(TileChoosed>-1)
				{
					//mThisGameTurn=Hexa::BLUE;
					//std::cout<<"   Accepted...: "<<std::endl;
					if(mGameTurn==Hexa::BLUE)
					{
						HexaTiles.at(TileChoosed)->setNumber(randvalue);
						HexaTiles.at(TileChoosed)->setColor(Hexa::BLUE);
						Game::AudioManager::GetInstance().PlaySound(Game::Audio::SOUND_CLINK);
						CheckNeighBorTiles(TileChoosed);

						mGameTurn=Hexa::GREEN;
						mNextGreenAnim->setPlaying();
						mNClosedTile++;
						
					}
					else if(mGameTurn==Hexa::GREEN)
					{
						HexaTiles.at(TileChoosed)->setNumber(randvalue);
						HexaTiles.at(TileChoosed)->setColor(Hexa::GREEN);
						Game::AudioManager::GetInstance().PlaySound(Game::Audio::SOUND_CLINK);
						CheckNeighBorTiles(TileChoosed);

						mGameTurn=Hexa::BLUE;
						mNextBlueAnim->setPlaying();
						mNClosedTile++;
						
					}
				}
				else
				{
					std::cout<<""<<std::endl;
				}
			}
	}
}

void PlayState::OnRender(SDL_Surface *p_SurfDisplay)
{
	//std::cout<<"PlayState::OnRender(SDL_Surface *p_SurfDisplay)  "<<std::endl;

		//background 
	GraphicsUtil::getInstance().Render_SDLSurface(p_SurfDisplay,mImage3,0,0);
	//gear blue
	//GraphicsUtil::getInstance().Render_SDLSurface(Surf_Display,mImgGearBlue,650,20);
	mGearBlueAnim->Render();
	GraphicsUtil::getInstance().DrawNumber(p_SurfDisplay,HexaTile::ColoredBlue,650+45,20+45);
	
	//gear green
	//GraphicsUtil::getInstance().Render_SDLSurface(Surf_Display,mImgGearGreen,650,120);
	mGearGreenAnim->Render();
	GraphicsUtil::getInstance().DrawNumber(p_SurfDisplay,HexaTile::ColoredGreen,650+45,120+45);
	
	//next green
	//GraphicsUtil::getInstance().Render_SDLSurface(Surf_Display,mImgNextGreen,650,220);
	//GraphicsUtil::getInstance().DrawNumber(Surf_Display,HexaTile::ColoredGreen,650+45,120+45);
	//next blue
	//GraphicsUtil::getInstance().Render_SDLSurface(Surf_Display,mImgNextBlue,650,320);
	//GraphicsUtil::getInstance().DrawNumber(Surf_Display,HexaTile::ColoredGreen,650+45,120+45);
	
	//hexa tiles
	//for(hexIter=HexaTiles.begin();hexIter!=HexaTiles.end();hexIter++)
	//{
	//	(*hexIter)->Render();
	//}

	mTilesAnim->Render();
	

	//animObj->setPlaying();
	//animObj->Render();
	if(mGameTurn==Hexa::BLUE)
	{//660,240
			mNextBlueAnim->Render();
			GraphicsUtil::getInstance().DrawNumber(p_SurfDisplay,randValue_A[Index_RandValue_A],660+35,240+35);
	}
	else if(mGameTurn==Hexa::GREEN)
	{
			mNextGreenAnim->Render();
			GraphicsUtil::getInstance().DrawNumber(p_SurfDisplay,randvalue,660+35,220+35);
	}
	mButtonBack->Render();
	mButtonSound->Render();
	mButtonNew->Render();
	mButtonConnect->Render();

	if(mIsGameOver)
	{
		mYouWin->Render();
		mYouLose->Render();
		mGameDraw->Render();
	}
	


	
}

void PlayState::OnCleanup()
{

	CleanGamePlay();
	
	SDL_FreeSurface(mImage1);
	SDL_FreeSurface(mImage2);
	SDL_FreeSurface(mImage3);
	SDL_FreeSurface(mImage4);

	SDL_FreeSurface(mImgGearBlue);
	SDL_FreeSurface(mImgGearGreen);

	SDL_FreeSurface(mImgBlue);
	SDL_FreeSurface(mImgGreen);
	SDL_FreeSurface(mDisplayPtr);

	delete animObj;

	delete mGearBlueAnim;
	delete mGearGreenAnim;

	delete mNextGreenAnim;
	delete mNextBlueAnim;

	delete mButtonBack;
	delete mButtonSound;
	delete mButtonNew;
	delete mButtonConnect;

	delete mYouWin;
	delete mYouLose;
	delete mGameDraw;
	delete mTilesAnim;
	GraphicsUtil::getInstance().ClearSDLSurfaces();
	
}


int PlayState::SolveTargetTile(int newNumb)
{
	int neighbor_this_tile[6];
	int defenseWeight=0;
	int offenseWeight=0;

	int maxDecweight=-1;
	int resultTile=0;

	std::vector<HexaTile*>::iterator iter_;

	for(iter_=HexaTiles.begin();iter_!=HexaTiles.end();iter_++)
	{
		int index_this_tile=(*iter_)->getIndex();

		neighbor_this_tile[0]=-1;
		neighbor_this_tile[1]=-1;
		neighbor_this_tile[2]=-1;
		neighbor_this_tile[3]=-1;
		neighbor_this_tile[4]=-1;
		neighbor_this_tile[5]=-1;
		this->GetNeighBor(index_this_tile,neighbor_this_tile);

		int numbToCompare=newNumb;
		int colToCompare=HexaTiles.at(index_this_tile)->getColor();

		int decweight=0;
		for(int n=0;n<6;n++)
		{
			if(neighbor_this_tile[n]>-1 && neighbor_this_tile[n]<((MAX_ROW*MAX_COL)-1))
			{
				int numb=HexaTiles.at(neighbor_this_tile[n])->getNumber();
				int col=HexaTiles.at(neighbor_this_tile[n])->getColor();
						
				if(numb<numbToCompare)
				{
					int divider_=numbToCompare-numb;
					if(col==Hexa::NORMAL)
						decweight+=4;
					else if(col!=mGameTurn)
						decweight+=30+30/(divider_);
					else if(col==mGameTurn)
						decweight+=2;
						
				}
				else if(numb>=numbToCompare)
				{
					if(col!=mGameTurn)
						decweight+=0;
					else if(col==mGameTurn)
						decweight+=20;
				}

		}///end for
		if(maxDecweight<decweight)
		{
			if(colToCompare==Hexa::NORMAL)
			{
				maxDecweight=decweight;
				resultTile=index_this_tile;
			}
		}
	}

	}
	cout<<"[ computer ] Number Now : "<<newNumb<<endl;

	return resultTile;
}

void PlayState::InitAnim(SDL_Surface* p_SurfDisplay)
{
	mGearBlueAnim= new AnimatedObject();
	mGearBlueAnim->setScreen(*p_SurfDisplay);
	mGearBlueAnim->setPosition(650,20);
	mGearBlueAnim->setSpeed(5);
	mGearBlueAnim->setVisibleWhenNotPlaying(true);


	SDL_Surface* temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearBlue_0.png",1);
	mGearBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearBlue_1.png",1);
	mGearBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearBlue_3.png",1);
	mGearBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearBlue_4.png",1);
	mGearBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearBlue_5.png",1);
	mGearBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearBlue_6.png",1);
	mGearBlueAnim->AddFrame(temp);

	mGearGreenAnim= new AnimatedObject();
	mGearGreenAnim->setScreen(*p_SurfDisplay);
	mGearGreenAnim->setPosition(650,120);
	mGearGreenAnim->setSpeed(5);
	mGearGreenAnim->setVisibleWhenNotPlaying(true);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearGreen_0.png",1);
	mGearGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearGreen_1.png",1);
	mGearGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearGreen_3.png",1);
	mGearGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearGreen_4.png",1);
	mGearGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearGreen_5.png",1);
	mGearGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GearGreen_6.png",1);
	mGearGreenAnim->AddFrame(temp);

	mNextGreenAnim= new AnimatedObject();
	mNextGreenAnim->setScreen(*p_SurfDisplay);
	mNextGreenAnim->setPosition(660,220);
	mNextGreenAnim->setSpeed(5);
	mNextGreenAnim->setVisibleWhenNotPlaying(true);
	mNextGreenAnim->setVisibleFrameWhenNotPlaying(4);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextGreen_0.png",1);
	mNextGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextGreen_1.png",1);
	mNextGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextGreen_2.png",1);
	mNextGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextGreen_3.png",1);
	mNextGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextGreen_4.png",1);
	mNextGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextGreen_5.png",1);
	mNextGreenAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextGreen_6.png",1);
	mNextGreenAnim->AddFrame(temp);

	mNextBlueAnim= new AnimatedObject();
	mNextBlueAnim->setScreen(*p_SurfDisplay);
	mNextBlueAnim->setPosition(660,240);
	mNextBlueAnim->setSpeed(5);
	mNextBlueAnim->setVisibleWhenNotPlaying(true);
	mNextBlueAnim->setVisibleFrameWhenNotPlaying(4);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextBlue_0.png",1);
	mNextBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextBlue_1.png",1);
	mNextBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextBlue_2.png",1);
	mNextBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextBlue_3.png",1);
	mNextBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextBlue_4.png",1);
	mNextBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextBlue_5.png",1);
	mNextBlueAnim->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\NextBlue_6.png",1);
	mNextBlueAnim->AddFrame(temp);


	//button back to main menu
	mButtonBack= new AnimatedObject();
	mButtonBack->setScreen(*p_SurfDisplay);
	mButtonBack->setPosition(PosInGameButtonX,PosInGameButtonY);
	mButtonBack->setSpeed(5);
	mButtonBack->setVisibleWhenNotPlaying(true);
	mButtonBack->setVisibleFrameWhenNotPlaying(0);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bpower.png",1);
	mButtonBack->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bpower02.png",1);
	mButtonBack->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bpower03.png",1);
	mButtonBack->AddFrame(temp);


	
	// -- button sound  --
	mButtonSound= new AnimatedObject();
	mButtonSound->setScreen(*p_SurfDisplay);
	mButtonSound->setPosition(PosInGameButtonX,PosInGameButtonY+(1*OffsetY_InGameButton));
	mButtonSound->setSpeed(5);
	mButtonSound->setVisibleWhenNotPlaying(true);
	mButtonSound->setVisibleFrameWhenNotPlaying(0);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bsound_on.png",1);
	mButtonSound->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bsound_hover.png",1);
	mButtonSound->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bsound_off.png",1);
	mButtonSound->AddFrame(temp);

	// -- button new  --
	mButtonNew= new AnimatedObject();
	mButtonNew->setScreen(*p_SurfDisplay);
	mButtonNew->setPosition(PosInGameButtonX,PosInGameButtonY+(2*OffsetY_InGameButton));
	mButtonNew->setSpeed(5);
	mButtonNew->setVisibleWhenNotPlaying(true);
	mButtonNew->setVisibleFrameWhenNotPlaying(0);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bnew_01.png",1);
	mButtonNew->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bnew_02.png",1);
	mButtonNew->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bnew_03.png",1);
	mButtonNew->AddFrame(temp);

		// -- button new  --
	mButtonConnect= new AnimatedObject();
	mButtonConnect->setScreen(*p_SurfDisplay);
	mButtonConnect->setPosition(PosInGameButtonX,PosInGameButtonY+(3*OffsetY_InGameButton));
	mButtonConnect->setSpeed(5);
	mButtonConnect->setVisibleWhenNotPlaying(true);
	mButtonConnect->setVisibleFrameWhenNotPlaying(0);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bconnect.png",1);
	mButtonConnect->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bconnect02.png",1);
	mButtonConnect->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\bconnect03.png",1);
	mButtonConnect->AddFrame(temp);

		// -- You WIn  --
	mYouWin= new AnimatedObject();
	mYouWin->setScreen(*p_SurfDisplay);
	mYouWin->setPosition(150,250);
	mYouWin->setSpeed(5);
	mYouWin->setVisibleWhenNotPlaying(false);
	mYouWin->setVisibleFrameWhenNotPlaying(4);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouWin05.png",1);
	mYouWin->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouWin04.png",1);
	mYouWin->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouWin03.png",1);
	mYouWin->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouWin02.png",1);
	mYouWin->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouWin01.png",1);
	mYouWin->AddFrame(temp);


	mYouLose= new AnimatedObject();
	mYouLose->setScreen(*p_SurfDisplay);
	mYouLose->setPosition(150,250);
	mYouLose->setSpeed(5);
	mYouLose->setVisibleWhenNotPlaying(false);
	mYouLose->setVisibleFrameWhenNotPlaying(4);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouLose05.png",1);
	mYouLose->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouLose04.png",1);
	mYouLose->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouLose03.png",1);
	mYouLose->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouLose02.png",1);
	mYouLose->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\YouLose01.png",1);
	mYouLose->AddFrame(temp);

	mGameDraw= new AnimatedObject();
	mGameDraw->setScreen(*p_SurfDisplay);
	mGameDraw->setPosition(150,250);
	mGameDraw->setSpeed(5);
	mGameDraw->setVisibleWhenNotPlaying(false);
	mGameDraw->setVisibleFrameWhenNotPlaying(4);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GameDraw05.png",1);
	mGameDraw->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GameDraw04.png",1);
	mGameDraw->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GameDraw03.png",1);
	mGameDraw->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GameDraw02.png",1);
	mGameDraw->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\GameDraw01.png",1);
	mGameDraw->AddFrame(temp);

}

void PlayState::GetNeighBor(int tileNumb,int neighbor_[])
{
	int px=HexaTiles.at(tileNumb)->getIndex()/MAX_ROW;
	if(px%2)
	{
		neighbor_[2]=tileNumb-MAX_ROW;
		neighbor_[4]=tileNumb+MAX_ROW;
	}
	else
	{
		neighbor_[2]=tileNumb-MAX_ROW-1;
		neighbor_[4]=tileNumb+MAX_ROW-1;
	}

	neighbor_[3]=neighbor_[2]+1;
	neighbor_[5]=neighbor_[4]+1;
	
	if((px%2!=0)  && (HexaTiles.at(tileNumb)->getIndex()%MAX_ROW==MAX_ROW-1))
	{
		neighbor_[3]=-1;
		neighbor_[5]=-1;
	}
	if((px%2==0)  && (HexaTiles.at(tileNumb)->getIndex()%MAX_ROW==0))
	{
		neighbor_[2]=-1;
		neighbor_[4]=-1;
	}
	neighbor_[0]=tileNumb-1;
	neighbor_[1]=tileNumb+1;

	if(tileNumb % MAX_ROW==0)
	{
		neighbor_[0]=-1;
	}
	if(tileNumb % MAX_ROW==MAX_ROW-1)
	{
		neighbor_[1]=-1;
	}
}

void PlayState::CreateHexaTiles(SDL_Surface* p_SurfDisplay)
{
	int _posx=0;
	int _posy=0;
	int index=0;
	int index2=0;
	for(int i=0;i<MAX_COL;i++)
	{
		for(int j=0;j<MAX_ROW;j++)
		{
			HexaTile* temp=new HexaTile();
			temp->setSDLSurface(*mImage1,*mImage4,*mImgBlue,*mImgGreen,*p_SurfDisplay);

			if(i%2==0)
			{	
				_posx=(i*48)+OffsetX; 
				_posy=j*63+OffsetY;
				//std::cout<<_posx<<"---"<<_posy<<std::endl;
				//int t=index%10;
				if(j==0)
				{
					mXLineBorder[index2++]=_posx;
				}
			}
			else
			{
				_posx=(i*48)+OffsetX;
				_posy=(j*63)+32+OffsetY;
				if(j==0)
				{
					mXLineBorder[index2++]=_posx;
				}
			}
			temp->setIndex(index++);
			temp->setPos(_posx,_posy);
			HexaTiles.push_back(temp); 
		}
	}
	mTilesAnim=new TileAnimAlgorithm_A();
	mTilesAnim->setTiles(HexaTiles);
}

//check : where is my mouse now
void PlayState::CheckCollision(float x,float y)
{
	
	if(x>630)
	{
		CheckButtonClicked();
		//mPointedTile=-1;
		return;
	}
	int tempPointedTile=mPointedTile;
	/// X part
	float pxBottom=0;
	int resultX=0;
	for(int i=0;i<MAX_COL+1;i++)
	{
		if((x>pxBottom))
		{
			if(x<mXLineBorder[i])
			{
				resultX=i;
			    break;
			}
			if(i==12)
			{
				resultX=i;
				break;

			}
		}
		pxBottom=mXLineBorder[i];
	}
	/// Y part
	int resultY=0;
	if(resultX%2)
	{
		float pyBottom=0;
		for(hexIter=HexaTiles.begin();hexIter!=HexaTiles.end();hexIter++)
		{
			int _index=(*hexIter)->getIndex();
			if(_index<11)
			{
				if(_index<10)
				{
					float py=(*hexIter)->getPosY();
					if(y<=py && y>=pyBottom)
					{
						resultY=(*hexIter)->getIndex();
						break;
					
					}
					pyBottom=py;
				}
				else
				{
					if(y>=pyBottom)
					{
						resultY=(*hexIter)->getIndex();
						break;
					}
				}
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		float pyBottom=0;
		for(hexIter=HexaTiles.begin();hexIter!=HexaTiles.end();hexIter++)
		{
			int u=(*hexIter)->getIndex();
			if(u>10 && u<21)
			{
				if(u<20)
				{
					float py=(*hexIter)->getPosY();
					if(y<=py && y>=pyBottom)
					{
						resultY=(*hexIter)->getIndex();
						resultY=resultY-10;
						break;
					
					}
					pyBottom=py;
				}
				else
				{
					if(y>=pyBottom)
					{
						resultY=(*hexIter)->getIndex();
						resultY=resultY-10;
						break;
					}
				}
			}
		}
	}
	//std::cout<<"X: "<<resultX<<"Y: "<<resultY<<std::endl;
	//if(mPointedTile<0 || mPointedTile>((MAX_ROW*MAX_COL)-1)) //if out of range , forget it
	//{
	//	mPointedTile=tempPointedTile;
	//	return;
	//}
	if(mPointedTile>-1)
	{
		HexaTiles.at(mPointedTile)->setColor(Hexa::NORMAL);
	}
	
	mPointedTile=(resultY-1)+((resultX-1)*10);
	
	if(mPointedTile>-1)
	{
		HexaTiles.at(mPointedTile)->setColor(Hexa::SHINY);
	}
}

void PlayState::CheckNeighBorTiles(int tileX)
{
	GetNeighBor(tileX,NeighBor);

	int numbToCompare=HexaTiles.at(tileX)->getNumber();
	int colToCompare=HexaTiles.at(tileX)->getColor();
	//std::cout<<"numbToCompare: "<<numbToCompare<<std::endl;
	int numbTileChange=0;
	for(int n=0;n<6;n++)
	{
		if(NeighBor[n]>-1 && NeighBor[n]<((MAX_ROW*MAX_COL)-1))
		{
			int numb=HexaTiles.at(NeighBor[n])->getNumber();
			//std::cout<<"NeighBor: "<<n<<" Val: "<<numb<<std::endl;
			if(numb==-1)
				continue;

			//substraction
			int col=HexaTiles.at(NeighBor[n])->getColor();
			if(col!=colToCompare)
			{
				if(numb<numbToCompare)
				{
					numbTileChange++;
					if(mGameTurn==Hexa::GREEN)
					{
						HexaTiles.at(NeighBor[n])->setColor(Hexa::GREEN,true);
						--HexaTile::ColoredBlue;
						mGearGreenAnim->setPlaying();
					}
					else if(mGameTurn==Hexa::BLUE)
					{
						HexaTiles.at(NeighBor[n])->setColor(Hexa::BLUE,true);
						--HexaTile::ColoredGreen;
						mGearBlueAnim->setPlaying();
					}
				}
			}
			//addition
			else if(col==colToCompare)
			{
				if(numb!=20)
				{
					numb+=1;
					HexaTiles.at(NeighBor[n])->setNumber(numb);
				}
			}
		}
	}

	if(numbTileChange>2 && mThisGameTurn==mGameTurn)
	{
		Game::AudioManager::GetInstance().PlaySound(Game::Audio::SOUND_MATCH);
	}
	else if(numbTileChange>2 && mThisGameTurn!=mGameTurn)
	{
		Game::AudioManager::GetInstance().PlaySound(Game::Audio::SOUND_WRONG);
	}
}

void PlayState::CheckButtonClicked()
{
	//=============
	if(mCursorXValue>Game::PosInGameButtonX && mCursorXValue<Game::PosInGameButtonX+75
	&& mCursorYValue>Game::PosInGameButtonY && mCursorYValue<Game::PosInGameButtonY+75)
	{
		mButtonBack->setVisibleFrameWhenNotPlaying(1);
		if(mCursorClicked)
		{
			mButtonBack->setVisibleFrameWhenNotPlaying(2);
			Game::StateManager::GetInstance().SetState(Game::State::MAINMENU);
		}
	} else
	{
		mButtonBack->setVisibleFrameWhenNotPlaying(0);
	}
	
	//=============
	if(mCursorXValue>Game::PosInGameButtonX && mCursorXValue<Game::PosInGameButtonX+75
	&& mCursorYValue>Game::PosInGameButtonY+(1*OffsetY_InGameButton) && mCursorYValue<Game::PosInGameButtonY+(1*OffsetY_InGameButton)+75)
	{
		mButtonSound->setVisibleFrameWhenNotPlaying(1);
		if(mCursorClicked)
		{
			mButtonSound->setVisibleFrameWhenNotPlaying(2);
		}
	} else
	{
		mButtonSound->setVisibleFrameWhenNotPlaying(0);
	}
	//=============
	if(mCursorXValue>Game::PosInGameButtonX && mCursorXValue<Game::PosInGameButtonX+75
	&& mCursorYValue>Game::PosInGameButtonY+(2*OffsetY_InGameButton) && mCursorYValue<Game::PosInGameButtonY+(2*OffsetY_InGameButton)+75)
	{
		mButtonNew->setVisibleFrameWhenNotPlaying(1);
		if(mCursorClicked)
		{
			mButtonNew->setVisibleFrameWhenNotPlaying(2);
			CleanGamePlay();
			InitGamePlay();
		}
	} else
	{
		mButtonNew->setVisibleFrameWhenNotPlaying(0);
	}
	
	//=============
	if(mCursorXValue>Game::PosInGameButtonX && mCursorXValue<Game::PosInGameButtonX+75
	&& mCursorYValue>Game::PosInGameButtonY+(3*OffsetY_InGameButton) && mCursorYValue<Game::PosInGameButtonY+(3*OffsetY_InGameButton)+75)
	{
		mButtonConnect->setVisibleFrameWhenNotPlaying(1);
		if(mCursorClicked)
		{
			mButtonConnect->setVisibleFrameWhenNotPlaying(2);
		}
	} else
	{
		mButtonConnect->setVisibleFrameWhenNotPlaying(0);
	}
}

void PlayState::InitPlayState()
{
	//
}

void PlayState::InitGamePlay()
{
	mMouseUp=false;
	mMouseDown=false;
	int choosedColor=Hexa::BLUE;
	mThisGameTurn=choosedColor;

	mPointedTile=0;
	CreateHexaTiles(mDisplayPtr);
	mGameTurn=Hexa::BLUE;
	mIntroStop=0;
	mNClosedTile=0;
	mIsGameOver=false;

	mYouWin->setVisibleWhenNotPlaying(false);
	mYouLose->setVisibleWhenNotPlaying(false);
	mGameDraw->setVisibleWhenNotPlaying(false);

	int ivalue=1;
	for(int i=0;i<60;i++)
	{
		randValue_A[i]=ivalue;
		randValue_B[i]=ivalue++;
		if(ivalue==20)
		{
			ivalue=1;
		}
	}
	// suffle
	for(int i=0;i<10000;i++)
	{
		srand(time(NULL)*rand());
	    int a=(rand()%59);
		int b=(rand()%59);
		int temp_=randValue_A[a];
		randValue_A[a]=randValue_A[b];
		randValue_A[b]=temp_;
	}
	for(int i=0;i<10000;i++)
	{
		srand(time(NULL)*rand());
	    int a=(rand()%59);
		int b=(rand()%59);

		a=(a+randValue_A[a])%59;
		b=(a+b+randValue_A[a])%59;
		int temp_=randValue_B[a];
		randValue_B[a]=randValue_B[b];
		randValue_B[b]=temp_;
	}
	Index_RandValue_A=0;
	Index_RandValue_B=0;
	randvalue=randValue_B[0];

	/*
	for(int i=0;i<60;i++)
	{

		cout<<"-- randValue_A: "<<i<<" : "<<randValue_A[i]<<endl;
		cout<<"-- randValue_B: "<<i<<" : "<<randValue_B[i]<<endl;
	}
	system("pause");
	*/
}

void PlayState::CleanGamePlay()
{
	for(hexIter=HexaTiles.begin();hexIter!=HexaTiles.end();hexIter++)
	{
		delete (*hexIter);
	}
	HexaTiles.clear();
}